#include "LGrammarRule.h"
#include "RandomUtil.h"
#include <ctime>
#include <cstdlib>

/*
struct LGrammarRule::LProductionPair {
	LProductionPair(std::string p, int w): production(p), weight(w) {}
	std::string production;
	int weight;
}
*/

LGrammarRule::LGrammarRule(char nt, std::string prod, int w) :
	nonTerminal(nt)
	{
		//srand(time(0));
		addProductionRule(prod, w);
	}

LGrammarRule::LGrammarRule(const LGrammarRule &rhs) :
	nonTerminal(rhs.nonTerminal),
	productionList(rhs.productionList) {
		//srand(time(0));
}

LGrammarRule& LGrammarRule::operator=(const LGrammarRule& rhs) {
	nonTerminal = rhs.nonTerminal;
	productionList = std::vector<std::string>(rhs.productionList);
	return *this;
}

LGrammarRule::~LGrammarRule() {

}

char LGrammarRule::getNonTerminal(void) {
	return nonTerminal;
}

std::string LGrammarRule::getProduction(void) {
	 //int r = rand() % productionList.size();
	int r = RandomUtil::random(0, productionList.size() - 1);
	return productionList[r];
}

void LGrammarRule::addProductionRule(std::string production, int weight) {
	for(int i=0; i < weight; i++) {
		productionList.push_back(production);
	}
}
/*
int main(int argc, const char* argv[]) {
	char i;
	int a=0, o=0, e=0;
	LGrammarRule rule1('L', "LAL", 2);
	rule1.addProductionRule("LOL", 1);
	rule1.addProductionRule("LEL", 1);

	for(int i = 0; i < 100; i++) {
		std::string rule = rule1.getProduction();
		std::cout << rule << "\n";
		if(rule[1] == 'A') a++;
		if(rule[1] == 'E') e++;
		if(rule[1] == 'O') o++;
	}
	std::cout << "a: " << a << " e: " << e << " o: " << o << "\n";
}
*/